<?php
/**
 * Processing Request
 *
 * @author Anggiajuang Patria <http://anggiaj.web.id/> 
 */

require_once 'config.php';
require_once 'functions.php';

// generate email image
if (isset($_POST['generate'])) {
	switch ($_POST['generate']) {
		
		// generate standard email image
		case 'standard':
			
			$username 	= trim($_POST['username']);
			$domain		= $_POST['domain'];
			$ext 		= $_POST['ext'];
			$email		= $username.'@'.$domain.$ext;
			
			if (empty($username)) {
				json_out('error', '<p>Email username cannot be empty.</p>');
			} else if (!validate_email($email)) {
				json_out('error', '<p>Your email address format is not correct.</p>');
			} else {
				// increasig counter
				increase_hits();
				// set $_SESSION vars 
				@session_start();
				$_SESSION['username']	= $username;
				$_SESSION['domain']		= $domain;
				$_SESSION['ext'] 		= $ext;
				
				$output = '<p>Your email image (Right Click The Image - Save Image As...)</p>';
				// get email image
				$output .= '<iframe src="email_image.php?mode=standard" frameborder="0"'; 
				$output .= 'height="50" width="100%" scrolling="auto"></iframe>';
					
				json_out('success', $output);
			}
			break;
			
		// generate custom email image
		case 'custom':
			
			$email = trim($_POST['email']);
			if (empty($email)) {
				json_out('error', '<p>Email address cannot be empty.</p>');
			} else if (!validate_email($email)) {
				json_out('error', '<p>Your email address format is not correct.</p>');
			} else {
				// increasig counter
				increase_hits();
				// set $_SESSION vars 
				@session_start();
				$_SESSION['email']			= $email;
				$_SESSION['font_family']	= $_POST['font_family'];
				$_SESSION['font_size'] 		= $_POST['font_size'];
				$_SESSION['color'] 			= $_POST['color'];
				$_SESSION['background'] 	= $_POST['background'];
				
				$output = '<p>Your email image (Right Click The Image - Save Image As...)</p>';
				// get email image
				$output .= '<iframe src="email_image.php?mode=custom" frameborder="0"'; 
				$output .= 'height="50" width="100%" scrolling="auto"></iframe>';
				
				json_out('success', $output);
			}
			break;
	}
// fetch select data
} else if (isset($_POST['domain'])) {
	echo json_encode($EMAIL_HOST[$_POST['domain']]['ext']);
}
?>